<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/12/012
 * Time: 11:20
 */

namespace app\project\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Figure extends Controller
{
    private $pro_id = 0;
    public function _initialize()
    {
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
        $pro_id = empty($_GET['pro_id'])?0: $_GET['pro_id'];
        $this->assign("pro_id",$pro_id);
    }


    /**
     * 形象进度
     */
    public function index(){
        $common = new Common();
        //查询项目
        $detail = $common->public_select("project_pro",['id'=>$_GET['pro_id']])->find();
        $this->assign("detail",$detail);
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['title'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $where['pro_id'] = $_GET['pro_id'];
                $list = $common->public_select("project_pro_figure",$where,null,$start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_pro_figure",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }

    /**
     * 形象进度视图
     */
    public function detail(){
        $common = new Common();
        if(!empty($_GET['id'])){
            $id = $_GET['id'];
            $detail = $common->public_select("project_pro_figure",['id'=>$id])->find();
            $detail['images'] = explode(";",$detail['images']);
            $this->assign('detail',$detail);
        }
        $template = empty($_GET['id'])?"addView":"saveView";
        return $this->fetch($template);
    }

    //形象进度详情
    public function showView(){
        $common = new Common();
        $id = $_REQUEST['id'];
        $detail = $common->public_select("project_pro_figure",['id'=>$id])->find();
        $detail['images'] = explode(";",$detail['images']);
        $this->assign('detail',$detail);
        return view("showView");
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $this->condition($data);
            if(!empty($data['images'])){
                $data['images'] = implode(';',$data['images']);
            }
            $data['addtime'] = date("Y-m-d h:i:s",time());
            $data['add_id'] = Session::get("id");
            $title = $common->public_select("project_pro",['id'=>$data['pro_id']])->value("title");
            $id = $common->handle("project_pro_figure",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】添加项目【'.$title.'】形象进度,关联id【" . $id . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改
     * @return mixed
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $this->condition($data);
            if(!empty($data['images'])){
                $data['images'] = implode(';',$data['images']);
            }
            $data['updatetime'] = date("Y-m-d h:i:s",time());
            $data['update_id'] = Session::get("id");
            $title =  $common->public_select("project_pro",['id'=>$data['pro_id']])->value("title");
            $common->handle("project_pro_figure",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】更新项目【'.$title.'】形象进度,关联id【" . $data['id'] . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除
     */
    public function del(){
        try{
            $common = new Common();
            $config = $common->selconfig();
            $id = $_POST['id'];
            $list = Db::connect($config)->name("project_pro_figure")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->where(['a.id'=>['in',$id]])
                ->field("b.title,a.id")->select();
            $res = $common->public_select("project_pro_figure",['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            foreach ($list as $k=>$v){
                $log['remark'] = "【工程管理】,账号【" . Session::get("user_name") . "】删除项目【".$v['title']."】形象进度,关联id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    /**
     * @param $data
     */
    public function condition($data){
        $rule = [
            ["title", "require", "项目名称不能为空"],
            ["content", "require", "主要建设内容不能为空"],
        ];
        $validate = new Validate($rule);
        if (!$validate->check($data)) {
            throw new Exception($validate->getError());
        }
    }

    //更新状态视图
    public function updateView(){
        //查询项目是否存在
        $id = $_REQUEST['id'];
        $common = new Common();
        $where['id'] = $id;
        $assign['detail'] = $common->public_select("project_pro",$where)->find();
        $this->assign($assign);
        return view("updateView");
    }

    //执行状态更新
    public function updateDo(){
        try{
            $common = new Common();
            $data = $this->request->post();
            $rule = [
                ["remark", "require", "问题内容不能为空"],
            ];
            $validate = new Validate($rule);
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }

            //更改项目状态
            $where['id'] = $data['id'];
            $update['project_status'] = $_POST['project_status'];
            $common->public_select("project_pro",$where)->update($update);

            //记录项目更新日志
            $insertData['pro_id'] = $data['id'];
            $insertData['remark'] = $data['remark'];
            $insertData['project_status'] = $data['project_status'];
            $insertData['addtime'] = date("Y-m-d H:i:s");
            $insertData['add_id'] = Session::get("id");
            $common->handle("project_pro_log",$insertData,1);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //更新日志
    public function updateLog(){
        $id = $_REQUEST['id'];
        $common = new Common();
        $where['id'] = $id;
        $assign['detail'] = $common->public_select("project_pro",$where)->find();
        $assign['pro_id'] = $id;
        $this->assign($assign);
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $where1['pro_id'] = $_REQUEST['id'];
                $list = $common->public_select("project_pro_log",$where1,null,$start,$limit)->order("id desc")->select();

                foreach ($list as $k=>$v){
                    $list[$k]['project_status'] = empty($v['project_status'])?"正常":"逾期";
                }
                $count = $common->public_select("project_pro_log",$where1)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return view("updateLog");
    }


    //固投信息
    public function fixedDetail(){
        $common = new Common();
        $id = $_POST['id'];
        //总投资额
        $assign['detail'] = $detail = Db::name("project_pro")->where(['id'=>$id])->find();
        //查询该报表是否存在
        $assign['form'] = $form = $common->public_select("project_pro_form",['pro_id'=>$id])->find();

        //已完成投资
        $arr = ['year_val','january_february','march','april','may','june','july','august','september','october','november','december'];
        $invested = 0;
        foreach ($arr as $item){
            if(!empty($form[$item])){
                $invested+=$form[$item];
            }
        }


        $assign['invested'] = $invested;
        $assign['percentage'] = round(($invested/$detail['investment'])*100,2);
        $assign['investment'] = $detail['investment'];

        $this->assign($assign);
        return view("fixedDetail");
    }

    //更新固投信息
    public function fixedApi(){
        $common = new Common();
        try {
            $id = $_POST['pro_id'];
            //查询该报表是否存在
            $data = $this->request->post();
            $detail_id = $common->public_select("project_pro_form",['pro_id'=>$id])->value("id");
            if(!empty($detail_id)){
                $data['id'] = $detail_id;
            }
            $common->handle("project_pro_form",$data,1);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


}